package ArraysAndSort;

import java.util.Arrays;

/**
 * Created by wangyg 
 * on 2017/6/16 14:10
 */
public class ArrayCopy {
	//指针拷贝
	public static void copy1() {
		String[] s1 = new String[] { "1", "2", "3" };
		String[] s2 = s1;
		s2[2] = "4";
		System.out.println(s1[2]);
	}

	//对象拷贝
	public static void copy2() {
		String[] s3 = new String[] { "4", "5", "6" };
		String[] s4 = new String[s3.length];
		for (int i = 0; i < s3.length; i++) {
			s4[i] = s3[i];
		}
		s4[2] = "-9999";
		System.out.println(s3[2]);
	}

	//	利用nativeMethod方法拷贝，和ArrayList中插入对象操作相同，利用System.arraycopy
	public static void copy3() {
		String[] s5 = new String[] { "7", "8", "9" };
		String[] s6 = new String[s5.length];
		System.arraycopy(s5, 0, s6, 0, s5.length);
		System.out.println(Arrays.toString(s6));
	}

	//利用Arrays中方法拷贝
	public static void copy4() {
		String[] s7 = new String[] { "10", "11", "12" };
		String[] s8 = Arrays.copyOf(s7, s7.length);
		System.out.println(Arrays.toString(s8));
	}

	//数组扩增,利用Arrays.copyOf方法，和ArrayList中grow方法相同
	public static void arrayIncream() {
		String[] s8 = new String[] { "heh", "hah", "nan" };
		String[] s9 = Arrays.copyOf(s8, s8.length + 5);
		System.out.println(Arrays.toString(s9));

	}

	public static void countSameChar() {
		String str = "统计一个字符在字符串中的位置";
		int[] s = count(str, '字');
		System.out.println(Arrays.toString(s));
	}

	//比较相同字符，同时扩容同时增加
	private static int[] count(String str, char c) {
		int[] array = {};
		for (int i = 0; i < str.length(); i++) {
			if (str.charAt(i) == c) {
				array = Arrays.copyOf(array, array.length + 1);
				array[array.length - 1] = i;
			}
		}
		return array;
	}

	static final int[] param = new int[] { 32, 14, 25, 20, 94, 1, 3, 84, 77, 53 };

	//冒泡排序
	public static void bubbleSort() {
		int[] integers = Arrays.copyOf(param, param.length);
		Integer temp;
		for (int i = 0; i < integers.length - 1; i++) {
			for (int j = i + 1; j < integers.length; j++) {
				if (integers[i] > integers[j]) {
					temp = integers[i];
					integers[i] = integers[j];
					integers[j] = temp;
				}
			}

		}
		System.out.println(Arrays.toString(integers));
	}

	//Arrays.sort排序，内部原理为BinarySort
	public static void sort(){
		int[] integers = Arrays.copyOf(param, param.length);
		Arrays.sort(integers);
		System.out.println(Arrays.toString(integers));
	}


	public static void BinarySort(){

	}
	public static void SelectionSort(){

	}

	/**
	 *
	 */
	public static void search(){
		int[] integers = Arrays.copyOf(param, param.length);
		Arrays.sort(integers);
		int i = Arrays.binarySearch(integers, 25);
		System.out.println(i);

	}
	public static void main(String[] args) {
		//		copy1();
		//		copy2();
		//		copy3();
		//		copy4();
		//		arrayIncream();
		//		countSameChar();
		//		bubbleSort();
//		sort();
//		System.out.println(Arrays.toString(param));
		search();

	}
}
